<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/11/13
 * Time: 23:13
 */
namespace app\admin\controller;

use code\Code;
use think\Controller;
use think\Db;
use think\Session;

class Login extends Controller
{
    //登录页面
    public function index()
    {
        return $this->fetch('login/index');
    }
    //登录判断
    public function postDologin(){
        if(!captcha_check(input('post.code'))){
            $data=[
                'status'=> 0,
                'msg' => '验证码错误，请重新输入！',
            ];
            return json($data);
        };
        /*$code=new Code();
        $re_code=$code->get();
        if(strtolower($re_code) != strtolower(input('post.code'))){
            $data=[
                'status'=> 0,
                'msg' => '验证码错误，请重新输入！',
            ];
        return json($data);
        }*/
        $username=input('post.username');
        $password=md5(input('post.password'));
        $admin=new \app\admin\model\Login();
        $name=$admin->where(['username'=>$username,'status'=>0])->find();
//            $adminrole=$admin->where('username',$username)->value('adminrole');

        if($name){
            $role= \app\admin\model\Admin::get(['username' => $username]);
            $adminrole=$role->adminrole;
            //得到名字正确情况下的密码
            $pass=$admin->where('username',$username)->value('password');
            $id=$admin->where('username',$username)->value('id');
            if($pass == $password){
                Session::set('username',$username);
                Session::set('adminrole',$adminrole);
//                $this->success('登录成功','admin/index/index','','1');
                //在登录成功后，需要把用户的权限管理模块放入到预设数组中
                $rolearr=Db::name('admin')
                    ->alias('a')
                    ->join('admin_role ar','a.id = ar.uid')
                    ->join('role r','ar.rid = r.id')
                    ->join('role_node rn','r.id = rn.rid')
                    ->join('nodes n','rn.nid = n.id')
                    ->where('a.id',$id)
                    ->column('n.name,n.mname,n.aname','');

                //设置变量存储参数
                //后台的进入权限
                $rolelist['index'][]='index';
                foreach ($rolearr as $value){
                    $rolelist[$value['mname']][]=$value['aname'];
                    if($value['aname']=='getadd'){
                        //给添加、上传图片
                        $rolelist[$value['mname']][]='postdoadd';
                        $rolelist[$value['mname']][]='postpics';
                    }elseif($value['aname']=='getedit'){
                        //给编辑、启用、停用、异步排序
                        $rolelist[$value['mname']][]='postdoedit';
                        $rolelist[$value['mname']][]='poststop';
                        $rolelist[$value['mname']][]='poststart';
                        $rolelist[$value['mname']][]='postupdate_order';
                        $rolelist[$value['mname']][]='getroles';
                        $rolelist[$value['mname']][]='postroles';
                        $rolelist[$value['mname']][]='getnodes';
                        $rolelist[$value['mname']][]='postnodes';
                    }elseif($value['aname']=='getindex'){
                        //给查看详情
                        $rolelist[$value['mname']][]='getdetail';
                    }elseif($value['aname']=='getdelete'){
                        //给批量删除
                        $rolelist[$value['mname']][]='getdatadel';
                        $rolelist[$value['mname']][]='postdelete';
                    }
                }
//                print_r($rolelist);
                Session::set('rolelist',$rolelist);
                $data=[
                    'status'=> 1,
                    'msg' => '登录成功',
                ];
                return json($data);
            }else{
                $data=[
                    'status'=> 0,
                    'msg' => '账号或密码错误，请重新输入！',
                ];
                return json($data);
            }
        }else{
            $data=[
                'status'=> 0,
                'msg' => '账号或密码错误，请重新输入！',
            ];
            return json($data);
        }
    }
    //退出
    public function getOutlogin(){
        Session::delete('username');
        Session::delete('rolelist');
        $this->redirect('/login');
    }
}